package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfVideocallInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.videocall.request.DeviceManageActDeviceRequest;
import com.ocom.common.request.videocall.request.DeviceManageOnShowRequest;
import com.ocom.common.request.videocall.request.DeviceManageUpdateDeviceRequest;
import com.ocom.common.request.videocall.request.GetVideocallListRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfVideocallInfo;
import com.ocom.payconfig.service.XfVideocallInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class XfVideocallInfoServiceImpl extends ServiceImpl<IDaoXfVideocallInfo, EntityXfVideocallInfo> implements XfVideocallInfoService {

    @Autowired
    private IDaoXfVideocallInfo dao;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfVideocallInfo> getVideocallList(Long companyId, DeviceManageOnShowRequest request) {
        QueryWrapper<EntityXfVideocallInfo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        if (StringUtils.isNotBlank(request.getGroupId())) {
            queryWrapper.eq("group_id", request.getGroupId());
        }
        if (StringUtils.isNotBlank(request.getSn())) {
            queryWrapper.eq("sn", request.getSn());
        }
        if (StringUtils.isNotBlank(request.getSnName())) {
            queryWrapper.like("sn_name", request.getSnName());
        }
        if (null != request.getExpireTime()) {
            queryWrapper.le("expire_time", request.getExpireTime());
        }
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean addVideocall(Long companyId, EntityXfVideocallInfo entity) {
        return this.save(entity);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateVideocall(Long companyId, DeviceManageUpdateDeviceRequest request) {

        UpdateWrapper<EntityXfVideocallInfo> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", companyId);

        if (StringUtils.isNotBlank(request.getSn())) {
            updateWrapper.eq("sn", request.getSn());
        }
        updateWrapper.set("sn_name", request.getSnName());
        if (StringUtils.isNotBlank(request.getRemark())) {
            updateWrapper.set("remark", request.getRemark());
        }

        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean updateExpireTime(Long companyId, String sn,  Date expireTime) {
        UpdateWrapper<EntityXfVideocallInfo> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", companyId);

        updateWrapper.eq("sn", sn);

        updateWrapper.set("expire_time", expireTime);

        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public boolean removeVideocall(Long companyId, String sn) {

        QueryWrapper<EntityXfVideocallInfo> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("sn", sn);

        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfVideocallInfo> getNewVideocallList(Long companyId, Long merchId) {
        return dao.getNewVideocallList(companyId, merchId);
    }
}
